//
//  SearchMailViewController.m
//  Utils
//
//  Created by enghou on 2018/4/13.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "SearchMailViewController.h"
@interface SearchMailViewController ()

@end

@implementation SearchMailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.beginField = [[UITextField alloc]init];
    [self.view addSubview:self.beginField];
    self.date = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    self.date.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
    self.date.datePickerMode = UIDatePickerModeDate;
    self.date.maximumDate = [NSDate date];
    self.date.backgroundColor = [UIColor whiteColor];
    self.beginField.inputView = self.date;
    self.beginField.inputAccessoryView = [self inputAccessView];
    
    self.endField = [[UITextField alloc]init];
    [self.view addSubview:self.endField];
    self.endDate = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    self.endDate.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
    self.endDate.datePickerMode = UIDatePickerModeDate;
    self.endDate.maximumDate = [NSDate date];
    self.endDate.backgroundColor = [UIColor whiteColor];
    self.endField.inputView = self.endDate;
    self.endField.inputAccessoryView = [self inputAccessView];
    self.title = @"收件消息查询";
    UIBarButtonItem *query = [[UIBarButtonItem alloc]initWithTitle:@"查询" style:UIBarButtonItemStylePlain target:self action:@selector(queryAction)];
    query.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = query;
}

-(void)queryAction{
    MailBoxViewController *mail = (MailBoxViewController *)self.theDelete;
    mail.mail_title = self.titleField.text;
    mail.sender = self.senderField.text;
    mail.msg_type = self.typeId;
    mail.beginTime = self.beginDateField.text;
    mail.endTime = self.endDateField.text;
    [mail beginRefresh];
    [self pop];
}

-(void)cancelAction{
    [self.view endEditing:YES];
}

-(void)sureAction{
    if ([self.beginField isFirstResponder]) {
        self.beginDateField.text = [[_date date]dateLineStrOnlyDay];
    }else if ([self.endField isFirstResponder]){
        self.endDateField.text = [[_endDate date]dateLineStrOnlyDay];
    }
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)getMessageType{
    if (!_messageTypes) {
        [[NetWorkerManager sharedInstance]queryMailTypeWithCondition:nil successBlock:^(BaseModel *model) {
            if (model.success) {
                self.messageTypes = model.list;
                [self alertWithTypes:self.messageTypes];
            }
        } failBlock:^(NSInteger code) {
            
        }];
    }else{
        [self alertWithTypes:self.messageTypes];
    }
}

-(void)alertWithTypes:(NSArray *)array{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];
    for (MessageTypeItemModel *model in array) {
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:model.name style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            self.typeId = model.code;
            self->_messageLabel.detailTextLabel.text = model.name;
        }];
        [alert addAction:cancel];
    }
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark - Table view data source

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 2) {
        [self getMessageType];
    }
}

- (IBAction)showBeginDate:(id)sender {
    [self.beginField becomeFirstResponder];
}
- (IBAction)showEndDate:(id)sender {
    [self.endField becomeFirstResponder];
}
@end
